package com.app.controller;

import static com.app.util.StringUtilsEx.isEmpty;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.app.bean.Movies;
import com.app.bean.News;
import com.app.bean.Ticket;
import com.app.mapper.NewsMapper;
import com.app.service.MovieService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Controller
public class IndexController {

	@Autowired
	private MovieService ms;
	
	@Autowired
	private NewsMapper newsMapper;
	
	@RequestMapping("front/index")
	public ModelAndView index(String moviename){
		ModelAndView mav = new ModelAndView();
		
		PageHelper.startPage(1, 10);
		List<Movies> list = ms.list(moviename);
		
		//电影
		mav.addObject("movieList",list);
		
		mav.addObject("jrpf",ms.queryList("jrpf"));
		mav.addObject("zsqd",ms.queryList("zsqd"));
		mav.addObject("hprc",ms.queryList("hprc"));
		mav.addObject("newsList",ms.queryList("news"));
		
		mav.setViewName("front/index.jsp");
		return mav;
	}
	
	
	@RequestMapping("front/detail")
	public ModelAndView detail(String id){
		ModelAndView mav = new ModelAndView();
		
		//根据id获取电影的所有信息
		Movies movie = ms.getById(id);
		
		//根据id获取所有的电影票
		List<Ticket> tickets = ms.getTicketsByMovieId(id);
		
		mav.addObject("movie", movie);
		mav.addObject("tickets", tickets);
		
		mav.addObject("jrpf",ms.queryList("jrpf"));
		mav.addObject("zsqd",ms.queryList("zsqd"));
		mav.addObject("hprc",ms.queryList("hprc"));
		mav.addObject("newsList",ms.queryList("news"));
		
		mav.setViewName("front/detail.jsp");
		return mav;
	}
	
	@RequestMapping("front/detailNews")
	public ModelAndView detailNews(String id){
		ModelAndView mav = new ModelAndView();
		
		//根据id获取电影的所有信息
		
		News news = newsMapper.selectByPrimaryKey(Integer.parseInt(id));
		
		mav.addObject("news", news);
		
		mav.addObject("jrpf",ms.queryList("jrpf"));
		mav.addObject("zsqd",ms.queryList("zsqd"));
		mav.addObject("hprc",ms.queryList("hprc"));
		mav.addObject("newsList",ms.queryList("news"));
		
		mav.setViewName("front/detailNews.jsp");
		return mav;
	}
	
	
	@RequestMapping("front/result")
	public ModelAndView result(String moviename,Integer page,Integer rows){
		ModelAndView mav = new ModelAndView();
		
		if(isEmpty(page) || isEmpty(rows)) {
			page = 1;
			rows = 5;
		}
		
		PageHelper.startPage(page, rows);
		
		//根据id获取所有的电影票
		List list = ms.list(moviename);
		mav.addObject("rows",list);
		PageInfo<Movies> pageInfo = new PageInfo<Movies>(list);
		mav.addObject("total",pageInfo.getTotal());
		mav.addObject("page",page);
		
		mav.addObject("jrpf",ms.queryList("jrpf"));
		mav.addObject("zsqd",ms.queryList("zsqd"));
		mav.addObject("hprc",ms.queryList("hprc"));
		mav.addObject("newsList",ms.queryList("news"));
		mav.setViewName("front/result.jsp");
		return mav;
	}
	
	
}
